package com.fyzk.activity.myview;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Paint.FontMetrics;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.FrameLayout;

import com.tyjc.lottery.R;
import com.fyzk.utils.AppUtil;

public class BallPopFrameLayout extends FrameLayout{
	
	private Context context;
	
	private int pmW,pmH,pmD1;
	private float pmD2;
	
	private Rect rect = new Rect(0, 0, 0, 0);
	private Bitmap qiu_bg_p;
	
	private String num = "";
	private int type = 0;
	
	private boolean isShowMoveBall;
	
	

	/**
	 * 构造函数
	 * @param context
	 * @param attrs
	 */
	public BallPopFrameLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		setWillNotDraw(false);

		if(isInEditMode()){
			return;
		}
		this.context = context;
		
		this.pmW = AppUtil.getWidth();
		this.pmH = AppUtil.getHeight();
		this.pmD1 = AppUtil.getDensityDpi();
		this.pmD2 = AppUtil.getDensity();
		
		qiu_bg_p = BitmapFactory.decodeResource(context.getResources(), R.drawable.hong_hover);
	}
	
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		Log.e("FramLayout   画球区", "l :"+rect.left+" r: "+rect.right+" t "+rect.top+" b "+rect.bottom);
		Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
		Paint paint_qiu = new Paint(Paint.ANTI_ALIAS_FLAG);
		
		paint.setColor(Color.BLACK);
		
		paint_qiu.setTextSize(22*pmD2);
		paint_qiu.setColor(Color.WHITE);
		paint_qiu.setTextAlign(Align.CENTER);
		Typeface font = Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD);
		paint_qiu.setTypeface( font );
		
		if (isShowMoveBall) {
			
			canvas.drawBitmap(qiu_bg_p, null, rect, paint);
			drawText(canvas, paint_qiu, ""+num, rect);
		}
		
	}
	
	/**
	 * 绘制文字
	 * @param canvas
	 * @param paint
	 * @param text
	 * @param rect
	 */
	private void drawText(Canvas canvas,Paint paint,String text,Rect rect){
		FontMetrics fm = paint.getFontMetrics();
		int baseLine = (int)(rect.top + (rect.bottom - rect.top - fm.bottom + fm.top) / 2 - fm.top);  
		canvas.drawText(text, rect.centerX(), baseLine, paint);
	}
	

	public Rect getRect() {
		return rect;
	}

	public void setRect(Rect rect, String num, int type, boolean isShowMoveBall) {
		this.rect = rect;
		this.num = num;
		this.type = type;
		this.isShowMoveBall = isShowMoveBall;
		switch (type) {
		case 0:
			qiu_bg_p = BitmapFactory.decodeResource(context.getResources(), R.drawable.hong_hover);
			break;
		case 1:
			qiu_bg_p = BitmapFactory.decodeResource(context.getResources(), R.drawable.lan_hover);
			break;
		default:
			qiu_bg_p = BitmapFactory.decodeResource(context.getResources(), R.drawable.lan_hover);
			break;
		}
		
		this.invalidate();
	}

	
	
}
